package com.yunyao.web.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.yunyao.common.web.util.YResult;
import com.yunyao.framework.dto.file.FileInfoDto;
import com.yunyao.framework.service.FileInfoService;
import com.yunyao.web.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/system/file")
public class fileController {
    @Autowired
    private FileInfoService fileInfoService;

    @PostMapping(value = "/upload")
    public YResult upload(@RequestParam("file") @Validated @NotNull MultipartFile file) throws IOException {
        if (null == file) {
            log.error("上传失败，请选择文件");
            return YResult.error("上传失败，请选择文件");
        }
        String fileName = file.getOriginalFilename();
        log.info("接收到图片上传请求||fileName={}", fileName);
        /**
         * 现在文件大小
         */
        if (file.getSize() > 20 * 1024 * 1024) {
            return YResult.error("上传失败，文件大小不能超过20M");
        }
        JSONObject ossResult = new JSONObject();
        try {

            FileInfoDto fileInfoDto = AliOssUtil.upload(file.getBytes(),
                    file.getOriginalFilename(), file.getContentType());
            if (null != fileInfoDto) {
                fileInfoDto.setFileSize(file.getSize());
                ossResult.put("uuidObjectName", fileInfoDto.getName());
                ossResult.put("name", fileInfoDto.getOriginalName());
                ossResult.put("url", fileInfoDto.getLink());
                ossResult.put("fileId", fileInfoService.saveFileInfo(fileInfoDto));

                ossResult.put("fileUrl", fileInfoDto.getLink());
                ossResult.put("fileOriginName", fileInfoDto.getOriginalName());
                ossResult.put("fileSuffix", fileInfoDto.getOriginalName().substring(fileInfoDto.getOriginalName().lastIndexOf(".")));
                return YResult.success(ossResult);
            } else {
                return YResult.error("连接文件服务器失败，请重试");
            }
        } catch (Exception e) {
            return YResult.error("上传失败，请重试");
        }
    }

    @DeleteMapping(value = "/deleteById")
    public YResult deleteById(@RequestParam @Validated @NotEmpty(message = "文件id不能为空") String fileId) {
        try {
            if (fileInfoService.deleteFileInfoById(fileId)) {
                return YResult.success("删除成功");
            } else {
                return YResult.error("删除失败，请重试");
            }
        } catch (Exception e) {
            return YResult.error("删除失败，请重试");
        }
    }

    @DeleteMapping(value = "/delete")
    public YResult delete(@RequestParam @Validated @NotEmpty(message = "uuidObjectName不能为空") String uuidObjectName) {
        try {
            if (AliOssUtil.delete(uuidObjectName)) {
                return YResult.success("删除成功");
            } else {
                return YResult.error("删除失败，请重试");
            }
        } catch (Exception e) {
            return YResult.error("删除失败，请重试");
        }
    }
}
